package vn.gq.udv.utils;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import vn.gq.udv.template.GQConst;
import android.content.Context;

public class FileHelper {
	
	private Context context;
	private static FileHelper instance;
	
	public FileHelper(Context context) {
		this.context = context;
	}
	
	public static FileHelper getInstance(Context context) {
		if (instance == null) {
			instance = new FileHelper(context);
		}
		return instance;
	}
	
	public void copy(String src, String dest) {
		if ("".equals(src)) return;
		FileOutputStream fos = null;
		File file = new File(dest);
		if (!file.getParentFile().isDirectory()) {
			file.getParentFile().mkdirs();
		}
		InputStream is = null;
		try {
			is = context.getAssets().open(src);
			fos = new FileOutputStream(file);
			byte[] buff = new byte[1024];
			int len = 0;
			while ((len = is.read(buff)) > 0) {
				fos.write(buff, 0, len);
			}
		} catch(IOException e) {
			e.printStackTrace();
		} finally {
			try { 
				is.close();
				if (fos != null) {
					fos.flush();
					fos.close();
				}
			} catch(IOException e) { 
			}
		}
	}
	
	public boolean isExist(String path) {
		File file = new File(path);
		return file.exists();
	}
	
	public boolean isExistsFileAsset(String fileName) {
		InputStream is = null;
		boolean ret = false;
		try {
			is = context.getAssets().open(fileName);
			if (is != null) ret = true;
		} catch(IOException e) {
		} finally {
			if (is != null) {
				try { is.close(); } catch(Exception e) {}
			}
		}
		return ret;
	}
	
	public String getFilePartName(String fileName, int idx) {
		String output = fileName;
		if (idx<10) {
			output = String.format("%s.00%d", fileName, idx);
		} else if (idx>=10 && idx<100) {
			output = String.format("%s.0%d", fileName, idx);
		} else if (idx>=100 && idx<1000) {
			output = String.format("%s.%d", fileName, idx);
		}
		return output;
	}
	
	public void mergeAndCopy(String src, String dest) {
		FileOutputStream fos = null;
		File file = new File(dest);
		if (!file.getParentFile().isDirectory()) {
			file.getParentFile().mkdirs();
		}
		try {
			fos = new FileOutputStream(dest);
			int idx = 1;
			while (true) {
				if (idx > 100) break;
				String fileName = getFilePartName(src, idx);
				if (!isExistsFileAsset(fileName)) break;
				InputStream is = null;
				try {
					is = context.getAssets().open(fileName);
					byte[] buff = new byte[1024];
					int len = 0;
					while ((len = is.read(buff)) > 0) {
						fos.write(buff, 0, len);
					}
				} finally {
					if (is != null) {
						is.close();
					}
				}
				idx++;
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) try { fos.flush(); fos.close(); } catch (IOException e) {}
		}
	}
	
	public void delete(String path) {
		try {
			File f = new File(path);
			if (f.isFile()) {
				f.delete();
			}
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
}
